package com.ruoyi.device.controller;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.security.PermissionUtils;
import com.ruoyi.device.domain.TmdDeviceInformation;
import com.ruoyi.device.service.ITmdDeviceInformationService;
import com.ruoyi.device.service.ITmdDeviceRecordService;
import com.ruoyi.device.vo.AllDeviceInfoVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import java.util.List;

import static com.ruoyi.common.utils.DateUtils.getNowDate;
import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 功能:
 * 作者:胡丁一
 * 日期:2024/4/30 15:42
 */
@Controller
@RequestMapping("/device/myUse")
public class TmdDeviceMyUseController  extends BaseController {

    private String prefix = "device/myUse";
    @Autowired
    private ITmdDeviceInformationService tmdDeviceInformationService;
    @Autowired
    private ITmdDeviceRecordService tmdDeviceRecordService;

    @RequiresPermissions("device:myUse:view")
    @GetMapping()
    public String information(){
        return prefix + "/myUse";
    }

    @RequiresPermissions("device:information:detail")
    @GetMapping("/detail/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        AllDeviceInfoVo allDeviceInfoVo = tmdDeviceRecordService.selectAllDeviceInfoVoById(id);
        TmdDeviceInformation tmdDeviceInformation = tmdDeviceInformationService.selectTmdDeviceInformationById(allDeviceInfoVo.getDeviceId());
        mmap.put("tmdDeviceInformation", tmdDeviceInformation);
        return prefix + "/detail";
    }

    @PostMapping("/ifs")
    @ResponseBody
    public AjaxResult dfs(@RequestParam("id") Long id) {
        try {

//            System.out.println(1111111);
            AllDeviceInfoVo allDeviceInfoVo1 = tmdDeviceRecordService.selectAllDeviceInfoVoById(id);
            //通过id查询对象  此次更新状态的这个id对象
            AllDeviceInfoVo allDeviceInfoVo = tmdDeviceInformationService.selectMyUseById(allDeviceInfoVo1.getDeviceId());
            //更改设备的使用状态
            tmdDeviceInformationService.InupdateStatus(allDeviceInfoVo1.getDeviceId());
            //新建一个vo类 vo类的内容是 上面查出来的
            //自己设置登录人 和 操作的类型 和操作的时间
            // 获取当前的用户信息
            SysUser currentUser = ShiroUtils.getSysUser();
            //设置操作人id
            allDeviceInfoVo.setOperatePerson(currentUser.getUserId());
            //设置操作时间
            allDeviceInfoVo.setOperateTime(getNowDate());
            //设置操作类型
            allDeviceInfoVo.setOperateName(0);
            //设置仪器id
            allDeviceInfoVo.setDeviceId(allDeviceInfoVo1.getDeviceId());
            //设置出库标志
            allDeviceInfoVo.setGooutFlag(1);
            //插入到sql中
            tmdDeviceRecordService.insertUseRecord(allDeviceInfoVo);
            return AjaxResult.success("仪器入库成功");
        }catch (Exception e) {
            return AjaxResult.error("仪器入库失败" + e.getMessage());
        }
    }
    /**
     * 查询仪器信息列表
     */
    @RequiresPermissions("device:myUse:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list() {
        startPage();
        // 获取当前的用户信息
        SysUser currentUser = ShiroUtils.getSysUser();
        Long userId = currentUser.getUserId();
        if (currentUser.isAdmin()){
            //如果是管理员 就查询所有数据
            List<AllDeviceInfoVo> list = tmdDeviceInformationService.selectAllTmdDeviceInformationList();
            return getDataTable(list);
        }else{
            //在查询的时候 在sql中查
            List<AllDeviceInfoVo> list = tmdDeviceInformationService.selectTmdDeviceInformationListByUserId(userId);
            return getDataTable(list);
        }
    }
}